home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectInput / Scrawl / scrawlb.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-08  |  5.8 KB  |  179 lines

  1. VERSION 5.00
  2. Begin VB.Form frmCanvas 
  3.    AutoRedraw      =   -1  'True
  4.    BackColor       =   &H80000005&
  5.    Caption         =   "Visual Basic Scrawl Sample"
  6.    ClientHeight    =   6150
  7.    ClientLeft      =   165
  8.    ClientTop       =   450
  9.    ClientWidth     =   9990
  10.    Icon            =   "ScrawlB.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   410
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   666
  15.    StartUpPosition =   2  'CenterScreen
  16.    Begin VB.Image imgPencil 
  17.       Appearance      =   0  'Flat
  18.       Height          =   480
  19.       Left            =   840
  20.       Picture         =   "ScrawlB.frx":0442
  21.       Top             =   600
  22.       Width           =   480
  23.    End
  24.    Begin VB.Menu mnuContext 
  25.       Caption         =   "none"
  26.       Visible         =   0   'False
  27.       Begin VB.Menu mnuSpeed1 
  28.          Caption         =   "Speed 1"
  29.       End
  30.       Begin VB.Menu mnuSpeed2 
  31.          Caption         =   "Speed 2"
  32.       End
  33.       Begin VB.Menu mnuSpeed3 
  34.          Caption         =   "Speed 3"
  35.       End
  36.       Begin VB.Menu Sep1 
  37.          Caption         =   "-"
  38.       End
  39.       Begin VB.Menu mnuClear 
  40.          Caption         =   "Clear"
  41.       End
  42.       Begin VB.Menu Sep2 
  43.          Caption         =   "-"
  44.       End
  45.       Begin VB.Menu mnuSuspend 
  46.          Caption         =   "Suspend"
  47.       End
  48.       Begin VB.Menu mnuExit 
  49.          Caption         =   "Exit"
  50.       End
  51.    End
  52. Attribute VB_Name = "frmCanvas"
  53. Attribute VB_GlobalNameSpace = False
  54. Attribute VB_Creatable = False
  55. Attribute VB_PredeclaredId = True
  56. Attribute VB_Exposed = False
  57. Implements DirectXEvent
  58. Dim Suspended As Boolean
  59. Private Sub DirectXEvent_DXCallback(ByVal eventid As Long)
  60.   Dim diDeviceData(1 To BufferSize) As DIDEVICEOBJECTDATA
  61.   Dim NumItems As Integer
  62.   Dim i As Integer
  63.   Dim windowRect As RECT
  64.   Static OldSequence As Long
  65.     On Error GoTo INPUTLOST
  66.     NumItems = objDIDev.GetDeviceData(diDeviceData, 0)
  67.     For i = 1 To NumItems
  68.       Select Case diDeviceData(i).lOfs
  69.         Case DIMOFS_X
  70.            g_cursorx = g_cursorx + diDeviceData(i).lData * g_Sensitivity
  71.            
  72.            ' We don't want to update the cursor or draw a line is response to
  73.            ' separate axis movements, or we will get a staircase instead of diagonal lines.
  74.            ' A diagonal movement of the mouse results in two events with the same sequence number.
  75.            If OldSequence <> diDeviceData(i).lSequence Then
  76.              UpdateCursor
  77.            End If
  78.            OldSequence = diDeviceData(i).lSequence
  79.          
  80.          Case DIMOFS_Y
  81.            g_cursory = g_cursory + diDeviceData(i).lData * g_Sensitivity
  82.            If OldSequence <> diDeviceData(i).lSequence Then
  83.              UpdateCursor
  84.            End If
  85.            OldSequence = diDeviceData(i).lSequence
  86.         
  87.          Case DIMOFS_BUTTON0
  88.            If diDeviceData(i).lData And &H80 Then
  89.              Drawing = True
  90.              CurrentX = g_cursorx
  91.              CurrentY = g_cursory
  92.            Else
  93.              Drawing = False
  94.            End If
  95.            
  96.          Case DIMOFS_BUTTON1
  97.            If diDeviceData(i).lData = 0 Then  ' button up
  98.              objDIDev.Unacquire
  99.              
  100.              ' Get the system cursor into the same position as the private cursor
  101.              Call GetWindowRect(hwnd, windowRect)
  102.              Call SetCursorPos(g_cursorx + windowRect.Left, g_cursory + windowRect.Top)
  103.              
  104.              ' Pop up menu at that position
  105.              Call PopupMenu(mnuContext)
  106.            End If
  107.            
  108.          
  109.        End Select
  110.     Next i
  111.   Exit Sub
  112. INPUTLOST:
  113. ' Since no events are signalled if the device is not acquired, this can only happen
  114. ' if the device is lost between signalling and retrieval.
  115.   If Err.Number = DIERR_INPUTLOST Then
  116.     objDIDev.Acquire
  117.   Else
  118.     Exit Sub
  119.   End If
  120. End Sub
  121. Public Sub UpdateCursor()
  122.   If g_cursorx < 1 Then g_cursorx = 1
  123.   If g_cursorx >= Canvas.ScaleWidth Then g_cursorx = Canvas.ScaleWidth - 1
  124.   If g_cursory < 1 Then g_cursory = 1
  125.   If g_cursory >= Canvas.ScaleHeight Then g_cursory = Canvas.ScaleHeight - 1
  126.   Canvas.imgPencil.Left = g_cursorx
  127.   Canvas.imgPencil.Top = g_cursory
  128.   If Drawing Then
  129.     Line -(g_cursorx, g_cursory)
  130.   End If
  131. End Sub
  132. Private Sub Form_Click()
  133. ' Allow user to resume drawing after suspending
  134.   Suspended = False
  135. End Sub
  136. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  137. ' This is a bit of a kludge. We need a way to force acquisition of the mouse whenever
  138. ' the context menu is closed, whenever we switch back to the application, or in any other
  139. ' circumstance where Windows is finished with the cursor. If a MouseMove event happens,
  140. ' we know the cursor is in our app window and Windows is generating mouse messages, therefore
  141. ' it's time to reacquire.
  142. ' Note: this event appears to happen even when there's no mouse activity, e.g. we have just
  143. ' Alt+Tabbed back, or cancelled out of the context menu with the Esc key.
  144.   If Suspended Then Exit Sub    ' Allow use of Windows cursor
  145.   On Error Resume Next
  146.   objDIDev.Acquire
  147. End Sub
  148. Private Sub mnuClear_Click()
  149.    Cls
  150. End Sub
  151. Private Sub mnuExit_Click()
  152.   End
  153. End Sub
  154. Private Sub mnuSpeed1_Click()
  155.   g_Sensitivity = 1
  156.   mnuSpeed1.Checked = True
  157.   mnuSpeed2.Checked = False
  158.   mnuSpeed3.Checked = False
  159.   objDIDev.Acquire
  160. End Sub
  161. Private Sub mnuSpeed2_Click()
  162.   g_Sensitivity = 2
  163.   mnuSpeed2.Checked = True
  164.   mnuSpeed1.Checked = False
  165.   mnuSpeed3.Checked = False
  166.   objDIDev.Acquire
  167. End Sub
  168. Private Sub mnuSpeed3_Click()
  169.   g_Sensitivity = 3
  170.   mnuSpeed3.Checked = True
  171.   mnuSpeed1.Checked = False
  172.   mnuSpeed2.Checked = False
  173.   objDIDev.Acquire
  174. End Sub
  175. Private Sub mnuSuspend_Click()
  176.   Suspended = True
  177.   objDIDev.Unacquire
  178. End Sub
  179.